﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchoolApplication.Models;

namespace GameSchoolApplication.Repositories
{

    public class MaterialRepository
    {
        gsDBDataContext link = new gsDBDataContext();

        /// <summary>
        /// TODO: Write a cool-ass description!
        /// As of right now this is just a dummy class 
        /// Precondition:
        /// Postcondition:
        /// Author:
        /// Date: 
        /// </summary>
        public Material getMaterialByID(int materialID)
        {
            Material mat = (from m in link.Materials
                              where m.matID == materialID
                              select m).First();
            return mat; 
        }

        /// <summary>
        /// TODO: Write a cool-ass description!
        /// As of right now this is just a dummy class 
        /// Precondition:
        /// Postcondition:
        /// Author:
        /// Date: 
        /// </summary>
        public IQueryable<Material> getAllMaterial()
        {
            return link.Materials;
        }
        /// <summary>
        /// TODO: Write a cool-ass description!
        /// As of right now this is just a dummy class 
        /// Precondition:
        /// Postcondition:
        /// Author:
        /// Date: 
        /// </summary>
        public bool addMaterial(Material mat)
        {
            try
            {
                link.Materials.InsertOnSubmit(mat);
            }
            catch (Exception)
            {
                return false;
            }
            return true; 
        }

        /// <summary>
        /// Editing material information
        /// Precondition: User must be an administrator/teacher and the material must exist
        /// Postcondition: Account information has been updated in the database
        /// Author: Aron Mar mod. by Alma
        /// Date: 7.5.2012
        /// </summary>
        public bool editMaterial(Material mat)
        {
            try
            {
                var mater = (from m in link.Materials
                           where m.matID == mat.matID
                           select m).First();

                mater.title = mat.title;
                mater.itemDesc = mat.itemDesc;
                mater.allowComments = mat.allowComments;
                mater.addedOn = mat.addedOn;

                save();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool addLecture(Lecture lect)
        {
            try
            {
                link.Lectures.InsertOnSubmit(lect);
            }
            catch (Exception)
            {
                return false; 
            }
            return true; 
        }

        /// <summary>
        /// Saves changes to database
        /// Precondition:
        /// Postcondition: Database has been updated
        /// Author: Aron og Bjarmi
        /// Date: 03.05.2012
        /// </summary>
        public void save()
        {
            link.SubmitChanges();
        }
    }
}